package com.c611.smartclassroom.service.Impl;

import com.c611.smartclassroom.mapper.RoleMapper;
import com.c611.smartclassroom.mapper.SysMapper;
import com.c611.smartclassroom.model.Role;
import com.c611.smartclassroom.model.RoleExample;
import com.c611.smartclassroom.model.Sys;
import com.c611.smartclassroom.model.SysExample;
import com.c611.smartclassroom.service.SysService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class SysServiceImpl implements SysService {

    @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
    @Autowired
    SysMapper sysMapper;
    @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
    @Autowired
    RoleMapper roleMapper;

    /**
     * 查询所有角色
     * @return
     */
    @Cacheable(value = "role",key = "'queryAllRole'",unless = "#result == null ")
    @Override
    public List<Role> queryAllRole() {

        //创建构造器，查询置废标识为 0 的所有记录
        RoleExample roleExample = new RoleExample();
        roleExample.createCriteria().andDeflagEqualTo(0);
        return roleMapper.selectByExample(roleExample);
    }

    /**
     * 增加角色
     * @param role
     * @return
     */
    @CacheEvict(value = "role",allEntries = true)
    @Override
    public int addRole(Role role) {
        return roleMapper.insertSelective(role);
    }


    /**
     * 根据roleid2来删除角色
     * @param roleid2
     * @return
     */
    @CacheEvict(value = "role",allEntries = true)
    @Override
    public int delRole(String roleid2) {
        Role role = new Role();
        RoleExample roleExample = new RoleExample();
        roleExample.createCriteria().andRoleid2EqualTo(roleid2).andDeflagEqualTo(0);
        role.setRoleid2(roleid2);
        //将置废标识标为 1
        role.setDeflag(1);

        return roleMapper.updateByExampleSelective(role,roleExample);
    }

    /**
     * 添加授权码
     * @param roleid2
     * @param auth
     * @return
     */
    @CacheEvict(value = "role",allEntries = true)
    @Override
    public int editAuth(String roleid2, String auth) {
        Role role = new Role();
        RoleExample roleExample = new RoleExample();
        roleExample.createCriteria().andRoleid2EqualTo(roleid2).andDeflagEqualTo(0);
        role.setAuth(auth);

        return roleMapper.updateByExampleSelective(role,roleExample);

    }

    /**
     * 添加每节课的上下课时间
     * @param course
     * @param stTime
     * @param edTime
     * @return
     */
    @CacheEvict(value = "sys",allEntries = true)
    @Override
    public int setCourseTime(String schoolId, String semesterID, Integer course, Date stTime, Date edTime) {
        SysExample sysExample = new SysExample();
        sysExample.createCriteria().andDelflagEqualTo(0).andSchoolidEqualTo(schoolId).andSemesteridEqualTo(semesterID);
        Sys sys = new Sys();
            //当传入的course参数不同，调用不同的方法，存入数据库中
            switch (course) {
                case 1:
                    sys.setC1start(stTime);
                    sys.setC1end(edTime);
                    break;
                case 2:
                    sys.setC2start(stTime);
                    sys.setC2end(edTime);
                    break;
                case 3:
                    sys.setC3start(stTime);
                    sys.setC3end(edTime);
                    break;
                case 4:
                    sys.setC4start(stTime);
                    sys.setC4end(edTime);
                    break;
                case 5:
                    sys.setC5start(stTime);
                    sys.setC5end(edTime);
                    break;
                case 6:
                    sys.setC6start(stTime);
                    sys.setC6end(edTime);
                case 7:
                    sys.setC7start(stTime);
                    sys.setC7end(edTime);
                    break;
                case 8:
                    sys.setC8start(stTime);
                    sys.setC8end(edTime);
                case 9:
                    sys.setC9start(stTime);
                    sys.setC9end(edTime);
                    break;
                case 10:
                    sys.setC10start(stTime);
                    sys.setC10end(edTime);
                    break;
                case 11:
                    sys.setC11start(stTime);
                    sys.setC11end(edTime);
                    break;
                case 12:
                    sys.setC12start(stTime);
                    sys.setC12end(edTime);
                    break;
                case 13:
                    sys.setC13start(stTime);
                    sys.setC13end(edTime);
                    break;
                case 14:
                    sys.setC14start(stTime);
                    sys.setC14end(edTime);
                    break;
                case 15:
                    sys.setC15start(stTime);
                    sys.setC15end(edTime);
                    break;

            }//switch

        int flag = sysMapper.updateByExampleSelective(sys, sysExample);
        if(flag == 0) {
            Sys newSys = new Sys();
            newSys.setSchoolid(schoolId);
            newSys.setSemesterid(semesterID);
            SysExample queryDate = new SysExample();
            queryDate.createCriteria().andSemesteridEqualTo(semesterID).andDelflagEqualTo(0);
            List<Sys> sysDate = sysMapper.selectByExample(queryDate);
            newSys.setSesttime(sysDate.get(0).getSesttime());
            newSys.setSeedtime(sysDate.get(0).getSeedtime());
            try {
                sys.setC1start(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").parse("2020-01-01 00:00:00"));
                sys.setC2start(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").parse("2020-01-01 00:00:00"));
                sys.setC3start(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").parse("2020-01-01 00:00:00"));
                sys.setC4start(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").parse("2020-01-01 00:00:00"));
                sys.setC5start(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").parse("2020-01-01 00:00:00"));
                sys.setC6start(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").parse("2020-01-01 00:00:00"));
                sys.setC7start(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").parse("2020-01-01 00:00:00"));
                sys.setC8start(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").parse("2020-01-01 00:00:00"));
                sys.setC9start(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").parse("2020-01-01 00:00:00"));
                sys.setC10start(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").parse("2020-01-01 00:00:00"));
                sys.setC1end(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").parse("2020-01-01 00:00:00"));
                sys.setC2end(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").parse("2020-01-01 00:00:00"));
                sys.setC3end(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").parse("2020-01-01 00:00:00"));
                sys.setC4end(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").parse("2020-01-01 00:00:00"));
                sys.setC5end(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").parse("2020-01-01 00:00:00"));
                sys.setC6end(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").parse("2020-01-01 00:00:00"));
                sys.setC7end(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").parse("2020-01-01 00:00:00"));
                sys.setC8end(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").parse("2020-01-01 00:00:00"));
                sys.setC9end(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").parse("2020-01-01 00:00:00"));
                sys.setC10end(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").parse("2020-01-01 00:00:00"));
            } catch (ParseException e) {
                e.printStackTrace();
            }
            sysMapper.insertSelective(newSys);
            return sysMapper.updateByExampleSelective(sys, sysExample);
        }
        return flag;
    }

    /**
     * 增加学期时间的方法
     * @param schoolId
     * @param semesterID
     * @param stTime
     * @param edTime
     * @return
     */
    @CacheEvict(value = {"semester","sys"},allEntries = true)
    @Override
    public int setSemesterDate(String schoolId, String semesterID, Date stTime, Date edTime) {

        Sys sys = new Sys();
        sys.setSchoolid(schoolId);
        sys.setSemesterid(semesterID);
        sys.setSesttime(stTime);
        sys.setSeedtime(edTime);

        try {
            sys.setC1start(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").parse("2020-01-01 00:00:00"));
            sys.setC2start(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").parse("2020-01-01 00:00:00"));
            sys.setC3start(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").parse("2020-01-01 00:00:00"));
            sys.setC4start(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").parse("2020-01-01 00:00:00"));
            sys.setC5start(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").parse("2020-01-01 00:00:00"));
            sys.setC6start(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").parse("2020-01-01 00:00:00"));
            sys.setC7start(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").parse("2020-01-01 00:00:00"));
            sys.setC8start(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").parse("2020-01-01 00:00:00"));
            sys.setC9start(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").parse("2020-01-01 00:00:00"));
            sys.setC10start(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").parse("2020-01-01 00:00:00"));
            sys.setC1end(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").parse("2020-01-01 00:00:00"));
            sys.setC2end(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").parse("2020-01-01 00:00:00"));
            sys.setC3end(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").parse("2020-01-01 00:00:00"));
            sys.setC4end(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").parse("2020-01-01 00:00:00"));
            sys.setC5end(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").parse("2020-01-01 00:00:00"));
            sys.setC6end(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").parse("2020-01-01 00:00:00"));
            sys.setC7end(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").parse("2020-01-01 00:00:00"));
            sys.setC8end(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").parse("2020-01-01 00:00:00"));
            sys.setC9end(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").parse("2020-01-01 00:00:00"));
            sys.setC10end(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").parse("2020-01-01 00:00:00"));
        } catch (ParseException e) {
            e.printStackTrace();
        }

        SysExample sysExample = new SysExample();
        sysExample.createCriteria().andDelflagEqualTo(0).andSchoolidEqualTo(schoolId).andSemesteridEqualTo(semesterID);

       return sysMapper.insertSelective(sys);
    }

    /**
     * 编辑学期时间
     * @param schoolId
     * @param semesterID
     * @param stTime
     * @param edTime
     * @return
     */
    @CacheEvict(value = {"semester","sys"},allEntries = true)
    @Override
    public int editSemesterDate(String schoolId, String semesterID, Date stTime, Date edTime) {
        SysExample sysExample = new SysExample();
        Sys sys = new Sys();

       //如果学校id及学期id匹配则更改开学时间和放假时间
        sysExample.createCriteria().andDelflagEqualTo(0).andSchoolidEqualTo(schoolId).andSemesteridEqualTo(semesterID);
        sys.setSesttime(stTime);
        sys.setSeedtime(edTime);
        return sysMapper.updateByExampleSelective(sys,sysExample);

    }

    /**
     * 删除学期时间
     * @param schoolId
     * @param semesterID
     * @return
     */
    @CacheEvict(value = {"semester","sys"},allEntries = true)
    @Override
    public int delSemesterDate(String schoolId, String semesterID) {
        SysExample sysExample = new SysExample();
        sysExample.createCriteria().andSemesteridEqualTo(semesterID).andSchoolidEqualTo(schoolId).andDelflagEqualTo(0);
        Sys sys = new Sys();
        //将置废标识置为 1
        sys.setDelflag(1);

        return sysMapper.updateByExampleSelective(sys,sysExample);
    }

    @Cacheable(value = "semester",key = "'querySemesterIdDistinct'",unless = "#result == null")
    @Override
    public List<String> querySemesterIdDistinct() {
        SysExample sysExample = new SysExample();
        sysExample.createCriteria().andDelflagEqualTo(0);
//        sysExample.setDistinct(true);
        sysExample.setOrderByClause("semesterID desc");
        List<Sys> list = sysMapper.selectByExample(sysExample);
        List<String> newList = new ArrayList<>();
        for (Sys sys : list) {
//            System.out.println(sys);
            if(!newList.contains(sys.getSemesterid()))
            newList.add(sys.getSemesterid());
        }
        return newList;
    }

    @Cacheable(value = "sys",key = "'queryDayTime::' + #schoolId + '::' + #semesterID",unless = "#result == null")
    @Override
    public Sys queryDayTime(String schoolId, String semesterID) {
        SysExample sysExample = new SysExample();
        sysExample.createCriteria()
                .andSchoolidEqualTo(schoolId)
                .andSemesteridEqualTo(semesterID)
                .andDelflagEqualTo(0);
        List<Sys> list = sysMapper.selectByExample(sysExample);
        return list.size() == 0 ? null : list.get(0);
    }

    @Cacheable(value = "sys",key = "'querySemeBySchid::' + #schoolId",unless = "#result == null")
    @Override
    public List<Sys> querySemeBySchid(String schoolId) {
        SysExample sysExample = new SysExample();
        sysExample.setOrderByClause("semesterID desc");
        sysExample.createCriteria()
                .andSchoolidEqualTo(schoolId)
                .andDelflagEqualTo(0);
        return sysMapper.selectByExample(sysExample);
    }

}



